﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using GPCSC.Model;
using System.Collections;
using NickLee.Common;
using System.Data;

namespace GPCSC.Web.SHFW
{
    public partial class SYS_addTZ : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.tzs.Value = Guid.NewGuid().ToString();
                F_bound();
                F_bounds();
            }
            //Button1.OnClientClick = Window1.GetSaveStateReference(labResult.ClientID) + Window1.GetShowReference("userlist.aspx", "用户列表");
            Button2.OnClientClick = Window1.GetShowReference("TZUpLoadFile.aspx?tzid="+tzs.Value, "附件上传");
            btnRegister.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");

        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            try
            {
                #region 记录信息主体
                CL_USER_S user = reUser();

                base.o_uiTable.Add("creaTs", DateTime.Now);
                base.o_uiTable.Add("creaTBy", user.REALNAME);
                base.o_uiTable.Add("creaTByOID", user.CL_USER_SOID.ToString());
                base.o_uiTable.Add("Title", Title.Text.Replace('\'','\"'));
                base.o_uiTable.Add("cont", cont.Text);
                base.o_uiTable.Add("CL_TZ_SOID", tzs.Value);

                GPCSC.Webs.databyhtable dh = new GPCSC.Webs.databyhtable();
                dh.databyhtables(base.o_uiTable, "add", "CL_TZ_S", "", "");
                #endregion
                #region 更新 发送人与接收人的数据表
                //string[] userlist = this.userids.Value.Split('#');
                //string[] username = this.usernames.Value.Split('#');
                int count = Grid1.SelectedRowIndexArray.Length;

                if (count > 0)
                {
                    foreach (int row in Grid1.SelectedRowIndexArray)
                    {
                        base.o_uiTable.Clear();
                        base.o_uiTable.Add("CLid", tzs.Value);
                        base.o_uiTable.Add("fsuserid", user.CL_USER_SOID.ToString());
                        base.o_uiTable.Add("fsname", user.REALNAME.ToString());
                        base.o_uiTable.Add("jsUserid", Grid1.DataKeys[row][0].ToString());
                        base.o_uiTable.Add("jsname", Grid1.DataKeys[row][1].ToString());
                        base.o_uiTable.Add("fsdate", DateTime.Now);

                        base.o_uiTable.Add("CLtitle", Title.Text);
                        dh.databyhtables(base.o_uiTable, "add", "CL_TZUSER", "", "");
                    }
                }
                #endregion
                Alert.Show("发送成功", "", "window.location.href ='SYS_TZ.aspx'");
            }
            catch (Exception ee)
            {
                throw ee;
            }

        }
        protected void Window1_Close(object sender, ExtAspNet.WindowCloseEventArgs e)
        {

           
        }

        public CL_USER_S reUser()
        {

            byte[] userInfo = (byte[])base.o_baseTable[c_obj.s_userInfo];
            Hashtable userInfoTable = CompressData.UnCompressHashtable(userInfo);
            CL_USER_S users = (GPCSC.Model.CL_USER_S)userInfoTable["SYS_USER"];
            return users;
        }


        #region

        /// <summary>
        /// 初始化所有人员列表
        /// </summary>
        protected void F_bound()
        {
            DataSet ds = new DataSet();
            Hashtable htable = new Hashtable();
            ds = new SqlReadXml().ReaderDataSet("sel_user_all", htable);
            if (ds.Tables.Count > 0)
            {
                Grid1.DataSource = ds.Tables[0];
                Grid1.DataBind();
            }

        }
        protected void F_bounds()
        {
            DataTable dtable = new DataTable();
            dtable.Columns.Add("id");
            dtable.Columns.Add("name");
            dtable.Columns.Add("level");
            #region 角色
            DataRow dr = dtable.NewRow();
            dr["id"] = "null";
            dr["name"] = "按角色选择";
            dr["level"] = 0;
            dtable.Rows.Add(dr);
            Hashtable htable = new Hashtable();
            DataSet ds = new DataReturn().ReturnData(htable, "sel_role_allct");
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow drs in ds.Tables[0].Rows)
                    {

                        dtable.Rows.Add(drs.ItemArray);
                    }
                }
            }
            #endregion
            #region 部门
            DataRow pdr = dtable.NewRow();
            pdr["id"] = "null";
            pdr["name"] = "按部门选择";
            pdr["level"] = 0;
            dtable.Rows.Add(pdr);
            Hashtable phtable = new Hashtable();
            DataSet pds = new DataReturn().ReturnData(phtable, "sel_POST_allct");
            if (pds.Tables.Count > 0)
            {
                if (pds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow pdrs in pds.Tables[0].Rows)
                    {
                        dtable.Rows.Add(pdrs.ItemArray);
                    }
                }
            }
            #endregion
            DDlist.DataSource = dtable;
            DDlist.DataBind();

        }
        protected void ttbSearch_Trigger1Click(object sender, EventArgs e)
        {
            F_bound();
        }

        protected void ttbSearch_Trigger2Click(object sender, EventArgs e)
        {

            string username = ttbSearch.Text.Trim();
            if (username == "")
            {
                F_bound();
            }
            else
            {
                Hashtable htable = new Hashtable();
                username = "%" + username + "%";
                htable.Add("@like", username);
                DataSet ds = new SqlReadXml().ReaderDataSet("sel_user_bylike", htable);
                if (ds.Tables.Count > 0)
                {
                    Grid1.DataSource = ds.Tables[0];
                    Grid1.DataBind();
                }
            }
        }
        protected void DDlist_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            string ids = DDlist.SelectedItem.Value;
            if (ids == "null")
            {
                F_bound();
            }
            else
            {
                string[] idt = ids.Split('#');
                //按部门
                Hashtable htable = new Hashtable();
                if (idt[1] == "1")
                {
                    htable.Clear();
                    htable.Add("@CL_POSTID_PK", idt[0]);
                    DataSet ds = new DataReturn().ReturnData(htable, "sel_user_bypost");
                    Grid1.DataSource = ds.Tables[0];
                    Grid1.DataBind();
                }
                else
                { 
                //按角色
                    htable.Clear();
                    htable.Add("@CL_ROLEOID_PK", idt[0]);
                    DataSet ds = new DataReturn().ReturnData(htable, "sel_user_byRole");
                    Grid1.DataSource = ds.Tables[0];
                    Grid1.DataBind();
                }
            }
           
           
        }


        #endregion

    }
}